# -*- coding:utf-8 -*-

from typing import TypeVar, Generic, Any
from pydantic import BaseModel, Field

from utils.logger import logger

# 定义一个 TypeVar，用于表示泛型类型
T = TypeVar('T', int, str, bool, dict, list, None, BaseModel)

class ResponseModel(BaseModel, Generic[T]):
    code: int = Field(default=200, description="状态码")
    success: bool = Field(default=True, description="是否成功")
    message: str = Field(default=True, description="信息")
    data: T = Field(default=None, description="数据")
    class Config:
        from_attributes = True

    @classmethod
    def successed(cls, code: int = 200, message: str="成功", data: dict={}):
        return ResponseModel(code=200, success=True, message=message, data=data)

    @classmethod
    def failed(cls, code: int = -1, message: str="失败", data: dict={}):
        logger.warning(message)
        return ResponseModel(code=code, success=False, message=message, data=None)

    @classmethod
    def unauthorized(cls, code: int = 401, message: str="登录信息已过期，访问系统资源失败", data: dict={}):
        return ResponseModel(code=code, success=False, message=message, data=data)

    @classmethod
    def forbidden(cls, code: int = 403, message: str="用户无此接口权限", data: dict={}):
        return ResponseModel(code=code, success=False, message=message, data=data)

    @classmethod
    def error(cls, code: int = 500, message: str="接口异常", data: dict={}):
        return ResponseModel(code=code, success=False, message=message, data=data)


class Page(BaseModel, Generic[T]):
    pageNo: int = Field(default=1, description="页码")
    pageSize: int = Field(default=10, description="每页条数")
    totalPage: int = Field(default=1, description="总页数")
    totalRows: int = Field(default=0, description="总条数")
    rows: list[T] = Field(default=[], description="数据")
    class Config:
        from_attributes = True

class ResponsePage(ResponseModel, Generic[T]):
    data: Page[T] = Field(default=None, description="数据")
    class Config:
        from_attributes = True


